//
//  ShopInfoView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/29.
//

#import "ShopInfoView.h"

@interface ShopInfoView ()

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *shopCountLabel;
@property (nonatomic, strong) UIButton *sendBtn;
@property (nonatomic, strong) UIButton *followBtn;
@property (nonatomic, strong) UIButton *setBtn;

@end

@implementation ShopInfoView

- (void)initDidLoad {
    self.backgroundColor = [UIColor whiteColor];
}
- (void)initViews {
    [self addSubview:self.avatarImageV];
    [self addSubview:self.nameLabel];
    [self addSubview:self.shopCountLabel];
    [self addSubview:self.sendBtn];
    [self addSubview:self.followBtn];
    [self addSubview:self.setBtn];
    
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(50);
        make.leading.mas_equalTo(16);
        make.top.mas_equalTo(4);
    }];
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.size.mas_equalTo(CGSizeMake(70, 24));
        make.centerY.equalTo(self.avatarImageV);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(4);
        make.leading.mas_equalTo(self.avatarImageV.mas_trailing).offset(8);
        make.trailing.mas_equalTo(self.sendBtn.mas_leading).offset(-4);
    }];
    [self.shopCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.avatarImageV.mas_bottom).offset(-4);
    }];
    [self.setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.width.height.mas_equalTo(20);
        make.centerY.equalTo(self.avatarImageV);
    }];
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.setBtn.mas_leading).offset(-12);
        make.size.mas_equalTo(CGSizeMake(64, 24));
        make.centerY.equalTo(self.avatarImageV);
    }];
    
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kRGB(0x1B1B1B);
        label.font = kFontSemiBold(16);
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 2;
        label.tag = 999+i;
        [label sizeToFit];
        [self addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(16+i*70+i*10);
            make.top.mas_equalTo(self.avatarImageV.mas_bottom).offset(20);
            make.width.mas_equalTo(70);
            make.height.mas_equalTo(50);
        }];
    }
}

- (void)setModel:(UserModel *)model {
    _model = model;
    [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text = model.nickName;
    
    self.shopCountLabel.text = [NSString stringWithFormat:@"%ld商品在售",model.enableCount];
    UILabel *label1 = [self viewWithTag:999];
    UILabel *label2 = [self viewWithTag:1000];
    UILabel *label3 = [self viewWithTag:1001];
    
    label1.text = [NSString stringWithFormat:@"%ld\n日访问",model.dayViews];
    label2.text = [NSString stringWithFormat:@"%ld\n月访问",model.monthViews];
    label3.text = [NSString stringWithFormat:@"%ld\n总访问",model.views];
    
    self.sendBtn.hidden = !model.isSelf;
    self.setBtn.hidden = (model.isSelf || !model.follow);
    self.followBtn.hidden = model.isSelf;
    self.followBtn.backgroundColor = model.follow ? kRGB(0xF2F2F2) : kRGB(0xFF6081);
    self.followBtn.selected = model.follow;
    if (!model.isSelf) {
        if (!self.setBtn.hidden) {
            [self.followBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.trailing.mas_equalTo(self.setBtn.mas_leading).offset(-12);
                make.size.mas_equalTo(CGSizeMake(64, 24));
                make.centerY.equalTo(self.avatarImageV);
            }];
        }else {
            [self.followBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.trailing.mas_equalTo(-16);
                make.size.mas_equalTo(CGSizeMake(64, 24));
                make.centerY.equalTo(self.avatarImageV);
            }];
        }
    }
}

#pragma mark - 懒加载
- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.layer.cornerRadius = 25;
        _avatarImageV.layer.masksToBounds = YES;
    }
    return _avatarImageV;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = kFontMediium(16);
        _nameLabel.textColor = kRGB(0x333333);
        _nameLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _nameLabel;
}

- (UILabel *)shopCountLabel {
    if (!_shopCountLabel) {
        _shopCountLabel = [[UILabel alloc] init];
        _shopCountLabel.textColor = kRGB(0xF15757);
        _shopCountLabel.font = kFontRegula(12);
        [_shopCountLabel sizeToFit];
    }
    return _shopCountLabel;
}

- (UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendBtn.backgroundColor = kBgColor;
        [_sendBtn setTitle:@"发布商品" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = kFontMediium(12);
        _sendBtn.layer.cornerRadius = 12;
        _sendBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_sendBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.publishGoodsBlock) {
                self.publishGoodsBlock();
            }
        }];
    }
    return _sendBtn;
}

- (UIButton *)followBtn {
    if (!_followBtn) {
        _followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_followBtn setTitle:@"+关注" forState:UIControlStateNormal];
        [_followBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_followBtn setTitleColor:kRGB(0x333333) forState:UIControlStateSelected];
        [_followBtn setTitle:@"已关注" forState:UIControlStateSelected];
        _followBtn.titleLabel.font = kFontMediium(12);
        _followBtn.backgroundColor = kRGB(0xFF6081);
        _followBtn.layer.cornerRadius = 12;
        _followBtn.layer.masksToBounds = YES;
        _followBtn.hidden = YES;
        @weakify(self)
        [[_followBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.followUserBlock) {
                self.followUserBlock(self.model);
            }
        }];
    }
    return _followBtn;
}

- (UIButton *)setBtn {
    if (!_setBtn) {
        _setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_setBtn setImage:[UIImage imageNamed:@"icon_set"] forState:UIControlStateNormal];
        _setBtn.hidden = YES;
        @weakify(self)
        [[_setBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.setFollowBlock) {
                self.setFollowBlock(self.model);
            }
        }];
    }
    return _setBtn;
}


@end
